package com.hmdp.service.impl;

import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Blog;
import com.hmdp.entity.User;
import com.hmdp.mapper.BlogMapper;
import com.hmdp.mapper.UserMapper;
import com.hmdp.service.IBlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.SystemConstants;
import com.hmdp.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Slf4j
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    IBlogService   blogService;

    @Autowired
    UserServiceImpl userService;
    @Override
    public Result queryById(Long id) {
        UserDTO user1 = UserHolder.getUser();
        if(user1==null){
            return Result.ok();
        }
        Blog blog = blogMapper.queryById(id);
        Long userId = blog.getUserId();
        User user = userMapper.selectById(id);
        String key="blog:like:"+id;
        blog.setIcon(user.getIcon());
        blog.setName(user.getNickName());
        Double member = redisTemplate.opsForZSet().score(key,user1.getId().toString());
        if(member==null){
            blog.setIsLike(false);
        }
        else
        {
            blog.setIsLike(true);
        }
        return Result.ok(blog);
    }

    @Override
    public Result queryHotBlog(Integer current) {
        UserDTO user1 = UserHolder.getUser();
        log.info("user1:{}",user1);
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .orderByDesc("liked")
                .page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        if(user1==null){
            return Result.ok(records);
        }
        // 查询用户
        records.forEach(blog ->{
            Long userId = blog.getUserId();
            User user = userService.getById(userId);
            blog.setName(user.getNickName());
            blog.setIcon(user.getIcon());
            String key="blog:like:"+blog.getId();
            Double member = redisTemplate.opsForZSet().score(key,user1.getId().toString());
            if(member==null){
                blog.setIsLike(false);
            }
            else
            {
                blog.setIsLike(true);
            }
        });
        return Result.ok(records);
    }

    @Override
    public void likeBlog(Long id) {
        UserDTO user = UserHolder.getUser();
        if(user==null){
            return ;
        }
        Long userId = user.getId();
        String key="blog:like:"+id;
        Blog blog = blogMapper.selectById(id);
        //查看用户是否点赞过
        Double member = redisTemplate.opsForZSet().score(key, userId.toString());
        //没有点过赞
        if(member==null){
            redisTemplate.opsForZSet().add(key,userId.toString(),System.currentTimeMillis());
            blogMapper.addLiked(id);
        }
        else
        {
            redisTemplate.opsForZSet().remove(key,userId.toString());
            blogMapper.subLiked(id);
        }
    }

    @Override
    public Result queryLikesById(Long id) {
        String key="blog:like:"+id;
        Set<String> range = redisTemplate.opsForZSet().range(key, 0, 4);
        List<User> users = new ArrayList<>();
        for(String item:range){
            Long userId = Long.valueOf(item);
            User user = userService.getById(userId);
            users.add(user);
        }

        return Result.ok(users);
    }

    @Override
    public Result queryUserBlogById(Long id) {
        PageHelper.startPage(1,10);
        List<Blog>list =  blogMapper.selectByUserId(id);
        PageInfo<Blog> pageInfo = new PageInfo<>(list);
        return Result.ok(pageInfo.getList());
    }
}
